#include "precompiled.h"

#include "CDBUpdater.h"

#include "CUtils.h"

#include <iostream>

bool CDBUpdater::update(const char* base_file, const char* update_file, const char* pub_key)
{
	if ( !CUtils::validateSign(update_file, pub_key) )
	{
		std::cout << "The signature mismatch." << std::endl;
		return false;
	}

	FILE * base = fopen(base_file, "w");
	if (!base) return false;

	FILE * upd = fopen(update_file, "r");
	if (!upd) return false;

	char buffer[4096] = {0};
	size_t bytes_read = 0;
	while ( (bytes_read = fread(buffer, 1, sizeof(buffer), upd)) )
	{
		fwrite(buffer, 1, bytes_read, base);
	}

	fclose(upd);
	fclose(base);

	return true;
}